package com.lucifer.treasure.enchant.effect;

import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;

/**
 * 🎨 统一的击中效果系统
 * 支持多种武器类型，使用不同的粒子效果
 */
public class HitEffect {
    

    /**
     * 🎨 统一的击中效果渲染方法
     * 根据武器类型使用不同的粒子效果，但保持相同的渲染结构
     */
    public static void renderHitEffect(ServerWorld world, Vec3d targetPos, Vec3d tip, Vec3d guard, Vec3d handle, Vec3d sideVector, SwordAuraEnchantmentEffect.WeaponType weaponType) {
        // 💥 目标位置爆发效果 - 根据武器类型使用不同粒子
        var primaryParticle = weaponType.particleType;
        var secondaryParticle = getSecondaryParticle(weaponType);

        // 主要粒子效果
        world.spawnParticles(primaryParticle, targetPos.x, targetPos.y, targetPos.z,
                getPrimaryParticleCount(weaponType),
                getPrimaryParticleSpread(weaponType),
                getPrimaryParticleSpread(weaponType),
                getPrimaryParticleSpread(weaponType),
                getPrimaryParticleSpeed(weaponType));

        // 次要粒子效果
        if (secondaryParticle != null) {
            world.spawnParticles(secondaryParticle, targetPos.x, targetPos.y, targetPos.z,
                    getSecondaryParticleCount(weaponType),
                    getSecondaryParticleSpread(weaponType),
                    getSecondaryParticleSpread(weaponType),
                    getSecondaryParticleSpread(weaponType),
                    getSecondaryParticleSpeed(weaponType));
        }

        // 🗡️ 武器主体渲染 - 统一的线条效果
        int segments = getWeaponSegments(weaponType);
        var weaponParticle = getWeaponBodyParticle(weaponType);

        for (int i = 0; i <= segments; i++) {
            double t = (double) i / segments;
            Vec3d weaponPos = guard.add(tip.subtract(guard).multiply(t));

            // 武器主体粒子
            world.spawnParticles(weaponParticle, weaponPos.x, weaponPos.y, weaponPos.z,
                    1, 0.01, 0.01, 0.01, 0);

            // 特殊武器的额外效果
            if (shouldRenderWeaponEdges(weaponType)) {
                double weaponWidth = getWeaponWidthAtPosition(weaponType, t);
                if (weaponWidth > 0.05 && i % 2 == 0) {
                    Vec3d leftEdge = weaponPos.add(sideVector.multiply(weaponWidth));
                    Vec3d rightEdge = weaponPos.subtract(sideVector.multiply(weaponWidth));

                    var edgeParticle = getEdgeParticle(weaponType);
                    world.spawnParticles(edgeParticle, leftEdge.x, leftEdge.y, leftEdge.z, 1, 0.01, 0.01, 0.01, 0.02);
                    world.spawnParticles(edgeParticle, rightEdge.x, rightEdge.y, rightEdge.z, 1, 0.01, 0.01, 0.01, 0.02);
                }
            }
        }

        // ⚡ 武器尖端特效
        var tipParticle = getTipParticle(weaponType);
        world.spawnParticles(tipParticle, tip.x, tip.y, tip.z,
                getTipParticleCount(weaponType),
                0.02, 0.02, 0.02,
                getTipParticleSpeed(weaponType));

        // 🎯 特殊武器的独特效果
        renderSpecialWeaponEffects(world, targetPos, tip, guard, handle, sideVector, weaponType);
    }

    // =================================
    // 🎨 粒子效果配置方法
    // =================================

    private static net.minecraft.particle.ParticleEffect getSecondaryParticle(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> ParticleTypes.CRIT;
            case AXE -> ParticleTypes.SWEEP_ATTACK;
            case WAND -> ParticleTypes.PORTAL;
            case ROD -> ParticleTypes.BUBBLE_POP;
            case ARROW -> ParticleTypes.SONIC_BOOM;
            case TRIDENT -> ParticleTypes.RAIN;
            case BOW -> ParticleTypes.NOTE;
            case STICK -> ParticleTypes.POOF;
            case TOOL -> ParticleTypes.CLOUD;
            default -> ParticleTypes.POOF;
        };
    }

    private static int getPrimaryParticleCount(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 8;
            case AXE -> 15;
            case WAND -> 12;
            case ROD -> 10;
            case ARROW -> 8;
            case TRIDENT -> 12;
            case BOW -> 8;
            case STICK -> 6;
            case TOOL -> 8;
            default -> 8;
        };
    }

    private static double getPrimaryParticleSpread(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 0.3;
            case AXE -> 0.5;
            case WAND -> 0.4;
            case ROD -> 0.3;
            case ARROW -> 0.2;
            case TRIDENT -> 0.4;
            case BOW -> 0.3;
            case STICK -> 0.2;
            case TOOL -> 0.3;
            default -> 0.3;
        };
    }

    private static double getPrimaryParticleSpeed(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 0.1;
            case AXE -> 0.2;
            case WAND -> 0.15;
            case ROD -> 0.1;
            case ARROW -> 0.15;
            case TRIDENT -> 0.1;
            case BOW -> 0.1;
            case STICK -> 0.08;
            case TOOL -> 0.1;
            default -> 0.1;
        };
    }

    private static int getSecondaryParticleCount(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 8;
            case AXE -> 3;
            case WAND -> 8;
            case ROD -> 8;
            case ARROW -> 2;
            case TRIDENT -> 6;
            case BOW -> 5;
            case STICK -> 4;
            case TOOL -> 6;
            default -> 5;
        };
    }

    private static double getSecondaryParticleSpread(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 0.3;
            case AXE -> 0.3;
            case WAND -> 0.3;
            case ROD -> 0.2;
            case ARROW -> 0.1;
            case TRIDENT -> 0.3;
            case BOW -> 0.2;
            case STICK -> 0.15;
            case TOOL -> 0.2;
            default -> 0.2;
        };
    }

    private static double getSecondaryParticleSpeed(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 0.1;
            case AXE -> 0.1;
            case WAND -> 0.1;
            case ROD -> 0.05;
            case ARROW -> 0.02;
            case TRIDENT -> 0.05;
            case BOW -> 0.05;
            case STICK -> 0.05;
            case TOOL -> 0.05;
            default -> 0.05;
        };
    }

    private static int getWeaponSegments(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 10;
            case AXE -> 8;
            case WAND -> 6;
            case ROD -> 10;
            case ARROW -> 12;
            case TRIDENT -> 10;
            case BOW -> 8;
            case STICK -> 8;
            case TOOL -> 6;
            default -> 8;
        };
    }

    private static net.minecraft.particle.ParticleEffect getWeaponBodyParticle(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> ParticleTypes.END_ROD;
            case AXE -> ParticleTypes.FIREWORK;
            case WAND -> ParticleTypes.ENCHANT;
            case ROD -> ParticleTypes.ITEM_SLIME;
            case ARROW -> ParticleTypes.ASH;
            case TRIDENT -> ParticleTypes.DRIPPING_DRIPSTONE_WATER;
            case BOW -> ParticleTypes.COMPOSTER;
            case STICK -> ParticleTypes.ASH;
            case TOOL -> ParticleTypes.FIREWORK;
            default -> ParticleTypes.SMOKE;
        };
    }

    private static boolean shouldRenderWeaponEdges(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return weaponType == SwordAuraEnchantmentEffect.WeaponType.SWORD ||
                weaponType == SwordAuraEnchantmentEffect.WeaponType.AXE;
    }

    private static double getWeaponWidthAtPosition(SwordAuraEnchantmentEffect.WeaponType weaponType, double t) {
        return switch (weaponType) {
            case SWORD -> (1.0 - t) * 0.15;
            case AXE -> (1.0 - t * 0.7) * 0.25;
            default -> 0.0;
        };
    }

    private static net.minecraft.particle.ParticleEffect getEdgeParticle(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> ParticleTypes.ELECTRIC_SPARK;
            case AXE -> ParticleTypes.LAVA;
            default -> ParticleTypes.ELECTRIC_SPARK;
        };
    }

    private static net.minecraft.particle.ParticleEffect getTipParticle(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> ParticleTypes.END_ROD;
            case AXE -> ParticleTypes.FIREWORK;
            case WAND -> ParticleTypes.DRAGON_BREATH;
            case ROD -> ParticleTypes.DRIPPING_WATER;
            case ARROW -> ParticleTypes.GLOW;
            case TRIDENT -> ParticleTypes.NAUTILUS;
            case BOW -> ParticleTypes.WHITE_ASH;
            case STICK -> ParticleTypes.ASH;
            case TOOL -> ParticleTypes.GLOW;
            default -> ParticleTypes.SMOKE;
        };
    }

    private static int getTipParticleCount(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 5;
            case AXE -> 3;
            case WAND -> 4;
            case ROD -> 3;
            case ARROW -> 3;
            case TRIDENT -> 2;
            case BOW -> 4;
            case STICK -> 2;
            case TOOL -> 3;
            default -> 3;
        };
    }

    private static double getTipParticleSpeed(SwordAuraEnchantmentEffect.WeaponType weaponType) {
        return switch (weaponType) {
            case SWORD -> 0.08;
            case AXE -> 0.03;
            case WAND -> 0.05;
            case ROD -> 0.02;
            case ARROW -> 0.05;
            case TRIDENT -> 0.03;
            case BOW -> 0.02;
            case STICK -> 0.01;
            case TOOL -> 0.03;
            default -> 0.02;
        };
    }

    private static void renderSpecialWeaponEffects(ServerWorld world, Vec3d targetPos, Vec3d tip, Vec3d guard, Vec3d handle, Vec3d sideVector, SwordAuraEnchantmentEffect.WeaponType weaponType) {
        switch (weaponType) {
            case SWORD -> {
                // 🗡️ 剑脊：蓝白中线
                for (int i = 0; i <= 5; i++) {
                    double t = (double) i / 5;
                    Vec3d spinePos = guard.add(tip.subtract(guard).multiply(t));
                    world.spawnParticles(ParticleTypes.SOUL_FIRE_FLAME, spinePos.x, spinePos.y, spinePos.z, 1, 0, 0, 0, 0);
                }
            }
            case ROD -> {
                // 🎣 钓线：从竿尖到目标的连线
                for (int i = 0; i <= 10; i++) {
                    double t = (double) i / 10;
                    Vec3d linePos = tip.add(targetPos.subtract(tip).multiply(t));
                    world.spawnParticles(ParticleTypes.DRIPPING_WATER, linePos.x, linePos.y, linePos.z, 1, 0.01, 0.01, 0.01, 0);
                }
            }
            case ARROW -> {
                // 🏹 箭羽：末端羽毛效果
                world.spawnParticles(ParticleTypes.WHITE_ASH, handle.x, handle.y, handle.z, 4, 0.03, 0.03, 0.03, 0.02);
            }
            case TRIDENT -> {
                // 🔱 三叉尖端：三个尖头
                Vec3d leftTine = tip.add(sideVector.multiply(0.1));
                Vec3d rightTine = tip.subtract(sideVector.multiply(0.1));
                world.spawnParticles(ParticleTypes.NAUTILUS, leftTine.x, leftTine.y, leftTine.z, 1, 0.01, 0.01, 0.01, 0.02);
                world.spawnParticles(ParticleTypes.NAUTILUS, rightTine.x, rightTine.y, rightTine.z, 1, 0.01, 0.01, 0.01, 0.02);
            }
            case BOW -> {
                // 🏹 弓弦震动
                for (int i = 0; i <= 6; i++) {
                    double t = (double) i / 6;
                    Vec3d stringPos = guard.add(tip.subtract(guard).multiply(t)).subtract(sideVector.multiply(0.15));
                    world.spawnParticles(ParticleTypes.WHITE_ASH, stringPos.x, stringPos.y, stringPos.z, 1, 0.005, 0.005, 0.005, 0);
                }
            }
            // 其他武器类型使用默认效果（不添加特殊效果）
        }
    }
} 